
    /**  
    * @Title: ConsumeBusLogQueue.java
    * @Package com.efast.cafe.commponent.log.queue
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年8月29日
    * @version V1.0  
    */
    
package com.efast.cafe.commponent.log.queue;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.commponent.log.constant.LogConstant;
import com.efast.cafe.commponent.log.entity.BusLogInfo;
import com.efast.cafe.commponent.log.service.LogAsyncService;
import com.efast.cafe.util.ConfigUtil;

/**
    * @ClassName: ConsumeBusLogQueue
    * @Description: 日志队列消费者
    * @author Administrator
    * @date 2018年8月29日
    *
    */
@Component
public class ConsumeBusLogQueue {

	@Autowired
	private LogAsyncService logAsyncService;
	
	@PostConstruct
    public void startrtThread() {
		int maxPool = Integer.parseInt(ConfigUtil.getValue(LogConstant.K_QUEUE_MAXPOOL));
        ExecutorService e = Executors.newFixedThreadPool(maxPool);//线程池
        for(int i=0;i<maxPool;i++){
        	e.submit(new PollBusLog(logAsyncService));
        }
    }
	
	class PollBusLog implements Runnable{
		LogAsyncService logAsyncService;
		
		public PollBusLog(LogAsyncService logAsyncService) {
            this.logAsyncService = logAsyncService;
        }
		    
		@Override
		public void run() {
			while(true){
				try {
					BusLogInfo salesLog = BusLogQueue.getInstance().poll();
                    if(salesLog!=null){
                    	logAsyncService.sendLog(salesLog);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
			}
			
		}
		
	}
}
